package com.informatics.polymer.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.informatics.polymer.client.PolymerTemplates.AlternatingCopolymerPanel;
import com.informatics.polymer.client.PolymerTemplates.BlockCopolymerPanel;
import com.informatics.polymer.client.PolymerTemplates.DendrimerCopolymerPanel;
import com.informatics.polymer.client.PolymerTemplates.GraftCopolymerPanel;
import com.informatics.polymer.client.PolymerTemplates.HomopolymerPanel;
import com.informatics.polymer.client.PolymerTemplates.RandomCopolymerPanel;
import com.informatics.polymer.client.SubstructureSearch.SubstructureSearchPanel;
import com.informatics.polymer.client.browserepository.BrowseRepositoryPanel;
import com.informatics.polymer.client.mltk.MachineLearningToolKitPanel;
import com.informatics.polymer.client.ontology.OntologyPanel;
import com.informatics.polymer.client.opsin.OPSINPanel;
import com.informatics.polymer.client.propertysearch.PropertySearchPanel;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class PolyGWTVersion2 implements EntryPoint {
		/**
		 * List of TreeRecord to contain the polymer templates in a tree grid.
		 */
		private TreeRecord[] polymerTemplates = new TreeRecord[6];
		/**
		 * HomopolymerPanel to be instantiated when a user clicks on the homopolymer value in the tree grid.
		 */
		private HomopolymerPanel homopolymerPanel;
		/**
		 * BlockCopolymerPanel to be instantiated when a user clicks on the block copolymer option in the tree grid.
		 */
		private BlockCopolymerPanel blockCopolymerPanel;
		/**
		 * AlternatingCopolymerPanel to be instantiated when a user clicks on the alternating copolymer option in the tree grid.
		 */
		private AlternatingCopolymerPanel alternatingCopolymerPanel;
		/**
		 * GraftCopolymerPanel to be instantiated when a user clicks on the graft copolymer option in the tree grid.
		 */
		private GraftCopolymerPanel graftCopolymerPanel;
		/**
		 * DendrimerPanel to be instantiated when a user clicks on the dendrimer option in the tree grid.
		 */
		private DendrimerCopolymerPanel dendrimerCopolymerPanel;
		/**
		 * RandomCopolymerPanel to be instantiated when a user clicks on the random copolymer option in the tree grid.
		 */
		private RandomCopolymerPanel randomCopolymerPanel;
		/**
		 * This is the entry point method.
		 */
		public void onModuleLoad() {
			final HLayout layout = new HLayout();
			final SectionStack sectionStack = new SectionStack();
			sectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);
			sectionStack.setWidth(200);
			sectionStack.setHeight(500);
			sectionStack.setBorder("2px solid #458B00");
			SectionStackSection section1 = new SectionStackSection("PolyGWT");
			section1.setExpanded(true);
			HTMLFlow welcome = new HTMLFlow();
			welcome.setOverflow(Overflow.AUTO);
			welcome.setPadding(10);
			String welcomeString = "Welcome to PolyGWT! Below you will find information on how to "
					+ "build polymers, search a database for repeat units based on substructure, search for "
					+ "repeat units based on property values and browse the existing repository of repeat units. Use OPSIN to convert " +
							"an IUPAC name to a Chemical Markup Language molecule. Click on the MLTK tab to check out our machine learning  toolkit "+
							"to cluster repeat units and visualize them or find repeat units nearest neighbours. Browse our ontologies by clicking on the ontology section.";
			welcome.setContents(welcomeString);
			section1.addItem(welcome);
			sectionStack.addSection(section1);
			sectionStack.setStyleName("sectionHeaderclosed");
			SectionStackSection section2 = new SectionStackSection(
					"Build A Polymer");
			section2.setExpanded(false);
			section2.setCanCollapse(true);
			Tree polymerTree = new Tree();
			setTreeNodes();
			polymerTree.setData(polymerTemplates);
			TreeGrid polymerTreeGrid = new TreeGrid();
			polymerTreeGrid.setWidth(200);
			polymerTreeGrid.setHeight(100);
			polymerTreeGrid.setFields(new TreeGridField("Polymer"));
			// Add cell clickhandler
			polymerTreeGrid.addCellClickHandler(new CellClickHandler() {
				@Override
				public void onCellClick(CellClickEvent event) {
					String polymerChosen = event.getRecord().getAttribute("Polymer");
					if (polymerChosen.equals("HomoPolymer")) {
						Window.alert("Homopolymer panel being created!");
						homopolymerPanel = new HomopolymerPanel();
						layout.addMember(homopolymerPanel);					
					} else if (polymerChosen.equals("BlockCopolymer")) {
						Window.alert("Block copolymer panel being created!");
						blockCopolymerPanel = new BlockCopolymerPanel();
						layout.addMember(blockCopolymerPanel);
					} else if (polymerChosen.equals("AlternatingCopolymer")) {
						Window.alert("Alternating copolymer panel being created!");
						alternatingCopolymerPanel = new AlternatingCopolymerPanel();
						layout.addMember(alternatingCopolymerPanel);
					} else if (polymerChosen.equals("GraftCopolymer")) {
						Window.alert("Graft copolymer panel being created!");
						graftCopolymerPanel = new GraftCopolymerPanel();
						layout.addMember(graftCopolymerPanel);
					} else if (polymerChosen.equals("RandomCopolymer")) {
						Window.alert("Random copolymer panel being created!");
						randomCopolymerPanel = new RandomCopolymerPanel();
						layout.addMember(randomCopolymerPanel);
					} else if (polymerChosen.equals("Dendrimer")) {
						Window.alert("Dendrimer polymer panel being created!");
						dendrimerCopolymerPanel = new DendrimerCopolymerPanel();
						layout.addMember(dendrimerCopolymerPanel);
					}
				}
			});
			polymerTreeGrid.setData(polymerTree);
			polymerTreeGrid.getData().openAll();
			polymerTreeGrid.draw();
			section2.addItem(polymerTreeGrid);
			sectionStack.addSection(section2);
			sectionStack.setStyleName("sectionHeaderclosed");
			SectionStackSection section3 = new SectionStackSection(
					"Substructure Search");
			section3.setExpanded(false);
			section3.setCanCollapse(true);
			Button ssButton = new Button("Search");
			ssButton.setSize("200", "30");
			ssButton.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					SubstructureSearchPanel substructureSearchPanel = new SubstructureSearchPanel();
					layout.addMember(substructureSearchPanel);
				}	
			});
			VLayout launchSubstructureVLayout = new VLayout();
			launchSubstructureVLayout.addChild(ssButton);
			section3.addItem(launchSubstructureVLayout);
			sectionStack.addSection(section3);
			sectionStack.setStyleName("sectionHeaderclosed");
			SectionStackSection section4 = new SectionStackSection(
					"Property Search");
			section4.setExpanded(false);
			section4.setCanCollapse(true);
			Button propertyButton = new Button("Search");
			propertyButton.setSize("200", "30");
			propertyButton.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					PropertySearchPanel propertySearchPanel = new PropertySearchPanel();
					layout.addMember(propertySearchPanel);
				}
			});
			VLayout launchPropertyVLayout = new VLayout();
			launchPropertyVLayout.addChild(propertyButton);
			section4.addItem(launchPropertyVLayout);
			sectionStack.addSection(section4);
			sectionStack.setStyleName("sectionHeaderclosed");
			SectionStackSection section5 = new SectionStackSection(
					"Browse Repository");
			section5.setExpanded(false);
			section5.setCanCollapse(true);
			Button browseButton = new Button("Browse Repository");
			browseButton.setSize("200", "30");
			browseButton.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					BrowseRepositoryPanel browseRepositoryPanel = new BrowseRepositoryPanel();
					layout.addMember(browseRepositoryPanel);
				}
			});
			VLayout launchBrowseVLayout = new VLayout();
			launchBrowseVLayout.addChild(browseButton);
			section5.addItem(launchBrowseVLayout);
			sectionStack.addSection(section5);
			//Add Opsin
			SectionStackSection opsinSection = new SectionStackSection("OPSIN");
			Button launchOpsinButton = new Button("Convert Name");
			launchOpsinButton.setSize("200", "30");
			launchOpsinButton.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					OPSINPanel opsinPanel = new OPSINPanel();
					layout.addMember(opsinPanel);
				}
			});
			VLayout launchOpsinVLayout = new VLayout();
			launchOpsinVLayout.addChild(launchOpsinButton);
			opsinSection.addItem(launchOpsinVLayout);
			sectionStack.addSection(opsinSection);
			//Machine Learning Toolkit
			SectionStackSection mlToolkit = new SectionStackSection("MLTK");
			Button launchMLTKButton = new Button("Launch Panel");
			launchMLTKButton.setSize("200", "30");
			launchMLTKButton.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					MachineLearningToolKitPanel mltk = new MachineLearningToolKitPanel();
					layout.addMember(mltk);
				}
			});
			VLayout launchMLTKVLayout = new VLayout();
			launchMLTKVLayout.addChild(launchMLTKButton);
			mlToolkit.addItem(launchMLTKVLayout);
			sectionStack.addSection(mlToolkit);
			//Ontologies
			SectionStackSection ontologySectionStack = new SectionStackSection("Ontologies");
			Button launchOntologyPanelButton = new Button("Launch Panel");
			launchOntologyPanelButton.setSize("200", "30");
			launchOntologyPanelButton.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					OntologyPanel ontologyPanel = new OntologyPanel();
					layout.addMember(ontologyPanel);
				}
			});
			VLayout launchOntologyLayout = new VLayout();
			launchOntologyLayout.addChild(launchOntologyPanelButton);
			ontologySectionStack.addItem(launchOntologyLayout);
			sectionStack.addSection(ontologySectionStack);
			sectionStack.setStyleName("sectionStack");
			sectionStack.setBackgroundColor("#F0F0F0");
			layout.setMembersMargin(20);
			layout.addMember(sectionStack);
			VLayout buttons = new VLayout();
			buttons.setMembersMargin(10);
			layout.addMember(buttons);
			layout.draw();
					}

		/**
		 * Method to populate the polymer templates in the accordion layout.
		 */
		private void setTreeNodes() {
			polymerTemplates[0] = new TreeRecord("HomoPolymer", "Polymer", "Ho");
			polymerTemplates[1] = new TreeRecord("BlockCopolymer", "Polymer", "BC");
			polymerTemplates[2] = new TreeRecord("AlternatingCopolymer", "Polymer",
					"AC");
			polymerTemplates[3] = new TreeRecord("GraftCopolymer", "Polymer", "GC");
			polymerTemplates[4] = new TreeRecord("RandomCopolymer", "Polymer", "Ro");
			polymerTemplates[5] = new TreeRecord("Dendrimer", "Polymer", "D");
	}
}
